const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
    list: [],
    selectIndex: '',
    showAddBtn: true,
    tips: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.type = options.type || 1;
    if (this.type == 1) {
      if (options.is_franchiser == 1 || options.is_repair) {
        this.setData({
          showAddBtn: false
        })
      }
      this.getAreaCodeList(options.id);
    } else {
      let _self = this;
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on('acceptDataFromOpenerPage', function (data) {
        _self.area_code_id = data.area_code_id;
        _self.getPartnerAreaCode(_self.area_code_id);
      })
    }
  },

  confirm() {
    let index = this.data.selectIndex;
    let code = this.data.list[index];
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromMiddleAreaCode', {
      ...code
    });
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 500)
  },

  filter(e) {
    const val = e.detail.value;
    const list = this.data.list;
    if (list.length <= 0) {
      return false;
    }
    list.filter(current => {
      if (current.name.includes(val) || current.area_code.includes(val)) {
        current.show = false;
      } else {
        current.show = true;
      }
    })
    this.setData({
      name: val,
      list
    })
  },

  //获取合伙人的区域码
  getPartnerAreaCode(area_code_id) {
    app.ajax({
      url: "Partner/CardMother/cardMotherList",
      data: {
        page: 1,
        pageSize: 999,
      },
      success: res => {
        if (res.data.code > 0) {
          let sources = res.data.data.data;
          let source = sources.filter(item => item.is_frozen == 1);
          let selectIndex = '';
          if (area_code_id !== "") {
            selectIndex = source.findIndex(item => item.id == area_code_id);
            if (~selectIndex) { //将选中数据排第一
              source.unshift(source.splice(selectIndex, 1)[0]);
              selectIndex = 0;
            } else {
              selectIndex = "";
            }
          }
          this.setData({
            empty: false,
            list: source,
            selectIndex,
          })
        } else {
          this.setData({
            empty: true,
            tips: "暂无区域码~"
          })
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },

  //根据设备获取区域码
  getAreaCodeList(equipment_control_id) {
    app.ajax({
      url: "Common/Equipment/getParterAreaCode",
      data: {
        equipment_control_id,
      },
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            empty: false,
            list: res.data.data
          })
        } else {
          this.setData({
            empty: true,
            tips: res.data.message
          })
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },

  chooseCode(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      selectIndex: index
    })

  },

  navAddAreaCode() {
    wx.navigateTo({
      url: '/pages/partner/regionCode/add/add?from=middle',
    })
  },

})